<template>
    <div class="content">
      <el-card class="box-card">
        <h2>参与者注册</h2>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-position="left"
          label-width="80px"
          class="demo-ruleForm"
        >
        <el-form-item label="学号" prop="stuid">
            <el-input v-model="ruleForm.stuid"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="parname">
            <el-input v-model="ruleForm.parname"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="学院" prop="institute">
            <el-input v-model="ruleForm.institute"></el-input>
          </el-form-item>
          <el-form-item label="专业" prop="major">
            <el-input v-model="ruleForm.major"></el-input>
          </el-form-item>
          <el-form-item label="年级" prop="grade">
            <el-input v-model="ruleForm.grade"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" prop="phonenumber">
            <el-input v-model="ruleForm.phonenumber"></el-input>
          </el-form-item>
        </el-form>
        <div class="btnGroup">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="goBack">返回</el-button>
        </div>
      </el-card>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入密码"));
        } else {
          if (this.ruleForm.checkPass !== "") {
            this.$refs.ruleForm.validateField("checkPass");
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          stuid:"",
          parname: "",
          pass: "",
          password: "",
          institute:"",
          major:"",
          grade:"",
          phonenumber:""
        },
        rules: {
          stuid: [
            { required: true, message: "学号不能为空！", trigger: "blur" },
          ],
          parname: [
            { required: true, message: "姓名不能为空！", trigger: "blur" },
          ],
          
          pass: [{ required: true, validator: validatePass, trigger: "blur" }],
          password: [
            { required: true, validator: validatePass2, trigger: "blur" },
          ],
          institute: [
            { required: true, message: "学院不能为空！", trigger: "blur" },
          ],
          major: [
            { required: true, message: "专业不能为空！", trigger: "blur" },
          ],
          grade: [
            { required: true, message: "年级不能为空！", trigger: "blur" },
          ],
          phonenumber: [
            { required: true, message: "电话号码不能为空！", trigger: "blur" },
          ],
        },
      };
    },
    methods: {
      submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        this.loading = true;  // 提交按钮显示加载动画
        if (valid) {
          let _this = this;
          this.axios({     // axios 向后端发起请求
<<<<<<< HEAD
            url: "/api/par/register",  // 请求地址
=======
            url: "http://localhost:8080/par/register",  // 请求地址
>>>>>>> 115e22a (更新)
            method: "post",             // 请求方法
            headers: {                  // 请求头
              "Content-Type": "application/json",
            },
            data: { // 请求参数，为 data，与登录的 params 不太一样
              stuid: _this.ruleForm.stuid,
              password: _this.ruleForm.password,
              parname:_this.ruleForm.parname,
              institute:_this.ruleForm.institute,
              major:_this.ruleForm.major,
              grade:_this.ruleForm.grade,
              phonenumber:_this.ruleForm.phonenumber,
            },
          }).then((res) => { // 当收到后端的响应时执行该括号内的代码，res 为响应信息，也就是后端返回的信息
            if (res.data.code === '0') {  // 当响应的编码为 0 时，说明注册成功
              // 显示后端响应的成功信息
              this.$message({
                message: res.data.msg,
                type: "success",
              });
            }else{  // 当响应的编码不为 0 时，说明注册失败
              // 显示后端响应的失败信息
              this.$message({
                message: res.data.msg,
                type: "warning",
              });
            }
            // 不管响应成功还是失败，收到后端响应的消息后就不再让登录按钮显示加载动画了
            _this.loading = false;
            console.log(res);
          });
        } else { // 如果账号或密码有一个没填，就直接提示必填，不向后端请求
          console.log("error submit!!");
          this.loading = false;
          return false;
        }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      goBack() {
        this.$router.go(-1);
      },
    },
  };
  </script>
  
  <style scoped>
  .content{
    width: 100%;
     height: 700px;
     background-image: url('/src/assets/img/注册.jpg');
     background-size:cover ;
     display: flex;
     justify-content: center;
     align-items: center;
 }
  .box-card {
    margin: auto auto;
    width: 400px;
  }
  .login-from {
    margin: auto auto;
  }
  </style>